<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        .text-center {
            text-align: center;
        }
        .box {
            width: 200px;
            height: 100px;
            background: linear-gradient(to right bottom, red,blue);

            margin-right: auto;
            margin-left: auto;
            /*margin: 0 auto;*/
        }

        .father {
            width: 100vw;
            height: 100px;
            background: #c3c3c3;

            display: flex;
            justify-content: center;  /*弹性内容水平居中*/
        }
        .father>.child {
            width: 30%;
            height: 50%;
            background: #3f983f;
        }

        .father2 {
             width: 100vw;
             height: 100px;
             background: #c3c3c3;

             display: grid;
             justify-items: center; /*网格内容水平居中*/
         }
        .father2>.child2 {
            width: 30%;
            height: 50%;
            background: #980914;
        }
        .father3 {
            width: 100vw;
            height: 100px;
            background: #c3c3c3;
            margin-bottom: 50px;
            position: relative;  /*父元素开启相对定位*/
        }
        .father3>.child3 {
            width: 30%;
            height: 50%;
            background: #980914;
            position: absolute; /*子元素开启绝对定位*/
            left: 50%;
            /*margin-left: -15vw;*/
            transform: translateX(-50%);
        }
    </style>
</head>
<body>
    <h1>元素水平居中</h1>

    <h2 class="text-center">文本内容居中</h2>
    <!--a img input button 全部都视为文本来处理-->
    <p class="text-center">
        <input type="text"> <button>按钮</button>
    </p>

    <h2>块级元素居中（需要设置宽度才能看到效果）</h2>
    <div class="box"></div>

    <h2>flex布局实现水平居中</h2>
    <div class="father">
        <div class="child"></div>
    </div>

    <h2>grid布局实现水平居中</h2>
    <div class="father2">
        <div class="child2"></div>
    </div>

    <h2>定位实现水平居中</h2>
    <div class="father3">
        <div class="child3"></div>
    </div>




</body>
</html>